package net.zoneland.knowledge.service.impl.search;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

/**
 * 问答处理链.
 *
 * @author: bcc
 * 2021/5/26
 */
@Service("searchManagerChain")
public class SearchOperationChain implements SearchOperation, ApplicationListener<ContextRefreshedEvent> {

    /**
     * 处理链.
     */
    private static final List<AbstractSearchOperation> searchOperations = new ArrayList<>();

    /**
     * 禁用词查询
     */
    @Autowired
    private SearchOperationImplQueryStopWords searchOperationImplQueryStopWords;

    /**
     * 表情问答处理.
     */
    @Autowired
    private SearchOperationImplQueryEmoji searchOperationImplQueryEmoji;

    /**
     * 点R慧办菜单，查询所有一级科目
     */
    @Autowired
    private SearchOperationImplQueryShowAll searchOperationImplQueryShowAll;

    @Autowired
    private SearchOperationImplQueryFixWords searchOperationImplQueryFixWords;

    @Autowired
    private SearchOperationImplQueryChat searchOperationImplQueryChat;

    @Autowired
    private SearchOperationImplQueryExact searchOperationImplQueryExact;

    @Autowired
    private SearchOperationImplQueryQuestion searchOperationImplQueryQuestion;

    @Autowired
    private SearchOperationImplDealResult searchOperationImplDealResult;

    @Autowired
    private SearchOperationImplQueryNavigatePageWords searchOperationImplQueryNavigatePageWords;

    /**
     * "我的收藏"问答.
     */
    @Autowired
    private SearchOperationImplQueryFavorite searchOperationImplQueryFavorite;



    @Override
    public void searchOrDeal(final SearchDto searchDto) throws Exception {
        searchOperations.get(0).searchOrDeal(searchDto);
    }

    /**
     * 设置处理链.
     */
    private static void setChain() {
        AbstractSearchOperation prevOperation = null;
        for (final AbstractSearchOperation searchOperation : searchOperations) {
            if (null != prevOperation) {
                prevOperation.setNextSearchOperation(searchOperation);
            }
            prevOperation = searchOperation;
        }
    }

    /**
        问答逻辑整理：
        1.判断是否违禁词（违禁词在后台字典配置），违禁词直接返回：未匹配到您的问题
        2.表情问题，如果问答内容是表情，直接返回表情。数据配置在后台字典,有数据不向后查询
        3.是否showAll查询：如果否怎么返回所有一级科目，适用RHB刚进去的场景，有数据不向后查询
        4.如果问答的是数字，根据问答上下文，将数字转换成问题题干
        5.闲聊库问答，匹配到就返回，查询到不向后查询
        6.精确匹配关键字，查询到不向后查询
        7.固定词组查询（固定词组配置在后台字段），查询到不向后查询
        8.分词查询
     */
    private void initChain() {
        searchOperations.add(searchOperationImplQueryChat);
        searchOperations.add(searchOperationImplQueryFavorite);
        searchOperations.add(searchOperationImplQueryStopWords);
        searchOperations.add(searchOperationImplQueryEmoji);
        searchOperations.add(searchOperationImplQueryShowAll);
        searchOperations.add(searchOperationImplQueryNavigatePageWords);
        searchOperations.add(searchOperationImplQueryFixWords);
        searchOperations.add(searchOperationImplQueryExact);
        searchOperations.add(searchOperationImplQueryQuestion);
        searchOperations.add(searchOperationImplDealResult);
    }

    @Override
    public void onApplicationEvent(final ContextRefreshedEvent event) {
        if (CollectionUtils.isEmpty(searchOperations)) {
            initChain();
            setChain();
        }
    }
}
